package com.wanggsx.library.base.views;

/**
 * Created by wanggsx on 15/9/29.
 * 波浪纹动画
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.wanggsx.library.util.UtilsSize;


public class PorterDuffXfermodeView extends View {
    public boolean isShowTimeText = true;
    public boolean isEnableAnim = true;
    private boolean isThreadPause = false;

    private static final int WAVE_TRANS_SPEED = 8;

    private Paint mBitmapPaint, mPicPaint;
    private int mTotalWidth, mTotalHeight;
    private int mCenterX, mCenterY;
    private int mSpeed;

    private Bitmap mSrcBitmap;
    private Bitmap mSrcBitmapTop;
    private Rect mSrcRect, mSrcRectTop, mDestRect;

    private PorterDuffXfermode mPorterDuffXfermode;
    private PorterDuffXfermode mPorterDuffXfermodeDouble;
    private Bitmap mMaskBitmap;
    private Rect mMaskSrcRect, mMaskDestRect;
    private PaintFlagsDrawFilter mDrawFilter;
    private View viewText = null;

    private float heightRat = 1.0f;

    private int mCurrentPosition;

    public PorterDuffXfermodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        initBitmap();
        mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP);
        mPorterDuffXfermodeDouble = new PorterDuffXfermode(PorterDuff.Mode.DARKEN);
        mSpeed = UtilsSize.dp2px(WAVE_TRANS_SPEED);
        mDrawFilter = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG, Paint.DITHER_FLAG);
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        // 为了保证效果的同时，尽可能将cpu空出来，供其他部分使用
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                    }
                    if (isEnableAnim) {
                        // 不断改变绘制的波浪的位置
                        mCurrentPosition += mSpeed;
                        postInvalidate();
                        isThreadPause = false;
                    } else {
                        isThreadPause = true;
                        try {
                            // 为了保证效果的同时，尽可能将cpu空出来，供其他部分使用
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }

            ;
        }.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isThreadPause && mSrcBitmapTop != null) {
            // 从canvas层面去除锯齿
            canvas.setDrawFilter(mDrawFilter);
            canvas.drawColor(Color.TRANSPARENT);
            mDestRect.top = (int) (mTotalHeight * (1 - heightRat));
        /*
         * 将绘制操作保存到新的图层
         */
            int sc = canvas.saveLayer(0, 0, mTotalWidth, mTotalHeight, null, Canvas.ALL_SAVE_FLAG);

            if (mCurrentPosition + 2 * mCenterX >= mSrcBitmap.getWidth()) {
                mCurrentPosition = 0;
            }

            // 设定要绘制的波纹部分
            mSrcRect.set((int) (mCurrentPosition + 2 * mCenterX), 0, mCurrentPosition, mTotalHeight);
            // 绘制波纹部分
            canvas.drawBitmap(mSrcBitmap, mSrcRect, mDestRect, mBitmapPaint);

            mBitmapPaint.setXfermode(mPorterDuffXfermodeDouble);
            mSrcRectTop.set(mCurrentPosition, 0, (int) (mCurrentPosition + 2 * mCenterX), mTotalHeight);
            canvas.drawBitmap(mSrcBitmapTop, mSrcRectTop, mDestRect, mBitmapPaint);

            // 设置图像的混合模式
            mBitmapPaint.setXfermode(mPorterDuffXfermode);
            // 绘制遮罩圆
            canvas.drawBitmap(mMaskBitmap, mMaskSrcRect, mMaskDestRect,
                    mBitmapPaint);

            mBitmapPaint.setXfermode(null);
            canvas.restoreToCount(sc);

            if (isShowTimeText && getTag() != null) {
                TextView textView = (TextView) (getTag());
                textView.buildDrawingCache();
                Bitmap bp = textView.getDrawingCache();
                canvas.drawBitmap(bp, mCenterX - bp.getWidth() / 2, mCenterY - bp.getHeight() / 2, mBitmapPaint);
            }
        }
    }

    public void setViewText(View view) {
        viewText = view;
    }


    public void setHeightRat(float rat) {
        if (rat <= 0) {
            return;
        }
        if (rat >= 1.0f) {

            heightRat = 1.0f;
        } else {
            heightRat = rat;
        }
    }


    // 初始化bitmap
    private void initBitmap() {
        if (mSrcBitmap != null) {
            mSrcBitmap = null;
        }
        if (mSrcBitmapTop != null) {
            mSrcBitmapTop = null;
        }
        if (mMaskBitmap != null) {
            mMaskBitmap = null;
        }

        mSrcBitmap = null;//？
        mSrcBitmapTop = null;//?
        mMaskBitmap = null;//?
    }

    // 初始化画笔paint
    private void initPaint() {

        mBitmapPaint = new Paint();
        // 防抖动
        mBitmapPaint.setDither(true);
        // 开启图像过滤
        mBitmapPaint.setFilterBitmap(true);

        mPicPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPicPaint.setDither(true);
        mPicPaint.setColor(Color.RED);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
        mCenterX = mTotalWidth / 2;
        mCenterY = mTotalHeight / 2;

        mSrcRect = new Rect();
        mSrcRectTop = new Rect();
        mDestRect = new Rect(0, (int) (mTotalHeight * (1 - heightRat)), mTotalWidth, mTotalHeight);

        int maskWidth = mMaskBitmap.getWidth();

        int maskHeight = mMaskBitmap.getHeight();
        mMaskSrcRect = new Rect(0, 0, maskWidth, maskHeight);
        mMaskDestRect = new Rect(0, 0, mTotalWidth, mTotalHeight);
    }

    public void recycleBitmap() {
        if (mSrcBitmap != null) {
            mSrcBitmap.recycle();
            mSrcBitmap = null;
        }
        if (mSrcBitmapTop != null) {
            mSrcBitmapTop.recycle();
            mSrcBitmapTop = null;
        }
        if (mMaskBitmap != null) {
            mMaskBitmap.recycle();
            mMaskBitmap = null;
        }
    }

}
